// pages/createPatient/select/select.js
// 选择绑定方式
const app = getApp()
const api = require('../../../config/api')
const request = require('../../../utils/request')
const { stringify } = require('../../../utils/util')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    idCard: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  setIdCard(e) {
    this.setData({
      idCard: e.detail.value,
    })
  },
  bindPatient: async function () {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'],
      success: async (res) => {
        let id = ''

        if (res.result.indexOf('zl') > -1) {
          let arr = res.result.split('-')
          id = arr[0]
        }

        if (!id) {
          wx.showToast({
            title: '无效二维码',
            icon: 'none',
          })
          return
        }
        const babyres = await request.get(api.patientDetail + '/' + id)
        if (babyres.httpStatus === 200) {
          app.globalData.createPatientType = 1
          wx.navigateTo({
            url: `/pages/createPatient/bind/bind?babyData=${stringify(
              babyres.data
            )}`,
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '无效二维码',
          icon: 'none',
        })
      },
    })
  },
  createPatient() {
    wx.navigateTo({
      url: '/pages/createPatient/create/create',
    })
  },
  // 跳转搜索档案号页面
  handleSearch() {
    app.globalData.createPatientType = 2
    wx.navigateTo({
      url: '/pages/createPatient/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
